const app = getApp();

import regeneratorRuntime from '../../../common/vendor/runtime';

Page({
  data: {
    activeSectionId: 0,
    showingDetail: false,
    categoryLists: [], //分类列表
    canReachBottom: true,
    page: 1,
    categoryid: '',
    categoryDetail: {},  //分类详情
    garbageList: [],  //该分类下的所有数据
  },

  onLoad() {
    this.init();
  },

  async init() {
    try {
      const categoryLists = await this.getCategoryLists();
      const categoryid = categoryLists[0] && categoryLists[0].id;
      console.log('=====categoryid',categoryid);
      
      this.setData({categoryid}, ()=> {
        this.getCategoryDetail();
        this.getSetting();
      });
    } catch (error) {
      console.log('=========init error', error);
    }
  },

  getCategoryLists() {
    return new Promise((resolve, reject) => {
      app.util.request({
        module: 'cm_garbage_query',
        url: 'entry/wxapp/index',
        data: {
          op: 'getCategoryLists'
        },
        showLoading: true,
        success: res => {
          const categoryLists = res.data.data || '';
          categoryLists && this.setData({
            categoryLists
          })
          resolve(categoryLists);
        },
        fail: res => {
          reject(res);
        }
      });
    })
  },

  getCategoryDetail() {
    let { page = 0, garbageList = [], categoryid: id =''} = this.data;
    app.util.request({
      module: 'cm_garbage_query',
      url: 'entry/wxapp/index',
      data: {
        op: 'getCategoryDetail',
        id,
        page
      },
      showLoading: true,
      success: res => {
        const { category = {}, garbage = [] } = res.data.data;

        category&&this.setData({
          categoryDetail: category
        })

        if(garbage.length<=0){
          this.setData({ canReachBottom: false });
          return;
        }

        if(page === 1){
          garbageList = garbage; 
        }else {
          garbageList =  [...garbageList, ...garbage];
        }

        this.setData({
          garbageList
        })
      },
      fail: res => {
        reject(res);
      }
    });
  },

  onShareAppMessage() {
    const { shareCover: imageUrl = '', shareTitle: title = '' } = this.data;
    return {
      title,
      path:'/cm_garbage_query/pages/search/search',
      imageUrl
    };
  },

  switchSection(e) {
    const { categoryid = '', index: activeSectionId = 0 } = e.currentTarget.dataset;

    this.setData({ activeSectionId,  page:1, canReachBottom: true, garbageList: [], categoryDetail: {}, categoryid }, () =>{
      this.getCategoryDetail();
    })
  },

  showDetail(e) {
    const { itemName: detailItemName = '' } = e.currentTarget.dataset;
    this.setData({
      showingDetail: true,
      detailItemName
    });
  },

  hideDetail(t) {
    this.setData({
      showingDetail: false
    });
  },

  getSetting() {
    app.util.request({
      module: 'cm_garbage_query',
      url: 'entry/wxapp/index',
      data: {
        op: 'getSetting'
      },
      showLoading: true,
      success: res => {
        const { share_cover: shareCover = '', share_title: shareTitle ='' } =  res.data.data;
       this.setData({
          shareCover,
          shareTitle
        })
      },
    });
  },

  onReachBottom() {
    let { page = 1, canReachBottom = true } = this.data;

    if(canReachBottom){
      this.setData({
        page: ++page
      })
      this.getCategoryDetail();
    }
  }
});